package com.ybxx.base.action;

import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;

import com.ybxx.base.model.Community;
import com.ybxx.base.model.ReadPerson;
import com.ybxx.base.service.ReadPersonService;
import com.ybxx.core.command.QueryFilter;
import com.ybxx.core.util.StringUtil;
import com.ybxx.core.util.UUIDGenerator;
import com.ybxx.core.web.action.BaseAction;
import com.ybxx.system.SystemConstants;
import com.ybxx.system.model.User;

public class ReadPersonAction extends BaseAction {
	@Resource
	private ReadPersonService readPersonService;
	private ReadPerson readPerson;
	private List<ReadPerson> readPersons;
	private JSONObject resultObj;
	private String msg="succ";
	/*加载初始化抄表人员数据*/
	public String printLoad(){
		readPersons=readPersonService.getAll();
		JSONArray arrayOne =new JSONArray();
		for(Iterator<ReadPerson> iter=readPersons.iterator();iter.hasNext();){
			ReadPerson readPerson=iter.next();
			JSONObject jsonObj=new JSONObject();
			jsonObj.put("id", readPerson.getPid());
			jsonObj.put("name",readPerson.getPname());
			jsonObj.put("pId","0");
			arrayOne.add(jsonObj);
		}
		resultObj=new JSONObject();
		resultObj.put("readPersons", arrayOne);
		return "printLoad";
	}
	/*加载修改信息*/
	public String view(){
		readPerson=readPersonService.get(readPerson.getPid());
		return "view";
	}
	/*删除*/
	public String remove(){
		try{
			
			readPersonService.remove(readPerson);
		}catch(Exception ex){
			msg="抄表人员信息删除失败.";
		}finally{
			return "succ";
		}
	}
	/*保存*/
	public String save(){
		try{
			if(StringUtils.isEmpty(readPerson.getPid())){
				readPerson.setPid(UUIDGenerator.getUUID());
			}
			readPersonService.save(readPerson);
		}catch(Exception ex){
			msg="抄表人员信息保存失败.";
		}finally{
			return "succ";
		}
	}
	/*加载修改信息*/
	public String load(){
		if(readPerson!=null && StringUtils.isNotEmpty(readPerson.getPid())){
			readPerson=readPersonService.get(readPerson.getPid());
		}
		return "load";
	}
	/**抄表人员管理，分页查询*/
	public String query(){
		QueryFilter filter = new QueryFilter(getRequest()); 
		this.readPersons=this.readPersonService.queryPage(filter, readPerson==null?null:readPerson.getPname());
		super.setRownum(filter.getPagingBean().getTotalItems());
		super.setCurrentpage(Integer.parseInt(filter.getPagingBean().getStart()+"")/Integer.parseInt(filter.getPagingBean().getPageSize()+"")+1);
		super.setPageSize(Integer.parseInt(filter.getPagingBean().getPageSize()+""));
		return "query";
	}
	public ReadPerson getReadPerson() {
		return readPerson;
	}
	public void setReadPerson(ReadPerson readPerson) {
		this.readPerson = readPerson;
	}
	public List<ReadPerson> getReadPersons() {
		return readPersons;
	}
	public void setReadPersons(List<ReadPerson> readPersons) {
		this.readPersons = readPersons;
	}
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	public JSONObject getResultObj() {
		return resultObj;
	}
	public void setResultObj(JSONObject resultObj) {
		this.resultObj = resultObj;
	}
	
}
